﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Data.SqlClient;

namespace printer
{
    public partial class Form1 : Form
    {
        int EmployeeId;
        bool old_owner_exist=false;
        bool login = false;
        const int manger_type_id = 1;
        const int employee_type_id = 2;
        const int studio_type_id = 3;
        const int studio_owner_type_id = 4;
        const string work_state_active = "active";
        const string work_state_notavtive = "not active";
        List<string> personsNames = new List<string>();
        reciptionist reciption_obj = new reciptionist();
        manager manger_object = new manager();
        Employee TempEmployee = new Employee();

        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// clear the content of the page exept the toolstrip that contain the icon
        /// </summary>
        /// <param name="page"></param>
        /// <param name="toolstrip"></param>
        void clear_content(ref TabPage page,ref ToolStrip toolstrip)
        {
            foreach (Control control1 in page.Controls)
            {
                if (control1 != toolstrip)
                {
                    page.Controls.RemoveByKey(control1.Name);
                }
            } 
        }

        bool is_studio_fields_empty(ref Panel panel,params TextBox[] txt)  //ayman sayed
        {
            bool is_field_empty = false;
            List<Label> lbl_validation = new List<Label>();
            bool first_run = true;
            List<string> panel_name = new List<string>();
            //if (!panel_name.Contains(panel.Name))
            //{
            //    panel_name.Add(panel.Name);
            //}
            //if (first_run && !panel_name.Contains(panel.Name))
            {
                for (int i = 0; i < txt.Length; i++)
                {
                    Label lbl = new Label();
                    lbl.Location = new Point(txt[i].Location.X - 100, txt[i].Location.Y);
                    lbl.Text = "";
                    lbl.ForeColor = Color.Red;
                    lbl_validation.Add(lbl);
                    panel.Controls.Add(lbl);

                }
                first_run = false;
            }

            for (int i=0;i<txt.Length;i++)
            {
                if (txt[i].Text == "")
                {
                    lbl_validation[i].Text = "*مطلوب";
                    is_field_empty = true;
                }
                else
                {
                    lbl_validation[i].Text = "";
                    is_field_empty = false;
                }
            }
            
            //if (txt_studio_name.Text == "")
            //{
            //    lbl_studio_name_notation.Text = "*مطلوب";
            //    is_field_empty = true;
            //}
            //else
            //{
            //    lbl_studio_name_notation.Text = "";
            //    is_field_empty = false;
            //}

            //if (txt_studio_address.Text == "")
            //{
            //    lbl_studio_address_notation.Text = "*مطلوب";
            //    is_field_empty = true;
            //}
            //else
            //{
            //    lbl_studio_address_notation.Text = "";
            //    is_field_empty = false;
            //}
            //if (txt_studio_phone.Text == "")
            //{
            //    lbl_studio_phone_notation.Text = "*مطلوب";
            //    is_field_empty = true;
            //}
            //else
            //{
            //    lbl_studio_phone_notation.Text = "";
            //    is_field_empty = false;
            //}
            return is_field_empty;
        }

        bool is_owner_fields_empty()//ayman sayed
        {
            bool is_field_empty = false;
            if (txt_owner_name.Text == "")
            {
                lbl_owner_name_notation.Text = "*مطلوب";
                is_field_empty = true;
            }
            else
            {
                lbl_owner_name_notation.Text = "";
                is_field_empty = false;
            }

            if (txt_owner_address.Text == "")
            {
                lbl_owner_address_notation.Text = "*مطلوب";
                is_field_empty = true;
            }
            else
            {
                lbl_owner_address_notation.Text = "";
                is_field_empty = false;
            }
            if (txt_owner_phone.Text == "")
            {
                lbl_owner_phone_notation.Text = "*مطلوب";
                is_field_empty = true;
            }
            else
            {
                lbl_owner_phone_notation.Text = "";
                is_field_empty = false;
            }
            return is_field_empty;
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            DataBase.connect("Studio_Service_Company");
            tollstrip_add_customer.Enabled = false;
            toolStrip_make_order.Enabled = false;
          
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello Ayman");
        }

        

        private void btn_login_Click_2(object sender, EventArgs e)
        {
            
                TempEmployee.User_Name = txt_user_name.Text;
                TempEmployee.PassWord= txt_pass.Text;
                bool login_result = TempEmployee.login(ref TempEmployee);
                if (login_result&&login==false)
                {

                    if (TempEmployee.Person_type_id == manger_type_id)
                    {
                        manger_object.ID = TempEmployee.ID;
                        manger_object.Addres = TempEmployee.Addres;
                        manger_object.Person_type_id = TempEmployee.Person_type_id;
                        manger_object.Name = TempEmployee.Name;
                        manger_object.PassWord = TempEmployee.PassWord;
                        manger_object.phone = TempEmployee.phone;
                        manger_object.Salary = TempEmployee.Salary;
                        manger_object.Ssn = TempEmployee.Ssn;
                        manger_object.User_Name = TempEmployee.User_Name;
                        manger_object.Work_Start_Time = TempEmployee.Work_Start_Time;


                        MessageBox.Show(manger_object.Name);
                    }
                    else if (TempEmployee.Person_type_id == employee_type_id)
                    {
                        reciption_obj.ID = TempEmployee.ID;
                        reciption_obj.Addres = TempEmployee.Addres;
                        reciption_obj.Person_type_id = TempEmployee.Person_type_id;
                        reciption_obj.Name = TempEmployee.Name;
                        reciption_obj.PassWord = TempEmployee.PassWord;
                        reciption_obj.phone = TempEmployee.phone;
                        reciption_obj.Salary = TempEmployee.Salary;
                        reciption_obj.Ssn = TempEmployee.Ssn;
                        reciption_obj.User_Name = TempEmployee.User_Name;
                        reciption_obj.Work_Start_Time = TempEmployee.Work_Start_Time;

                        MessageBox.Show(reciption_obj.Name);
                    }
                    else
                    {

                    }
                    clear_content(ref this.tabPage1, ref this.toolStrip2);
                    toolStrip_make_order.Enabled = true;
                    tollstrip_add_customer.Enabled = true;
                    login = true;
                    toolStripButton_login.Text = "تسجيل الخروج";
                }
                else
                {
                    MessageBox.Show("Wrong User Name or password");
                }
            
        }  //end of the login button click

        private void tollstrip_add_customer_Click(object sender, EventArgs e)
        {
          clear_content(ref this.tabPage1, ref this.toolStrip2);
          this.tabPage1.Controls.Add(panel_add_customer);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          //  this.tabPage1.Controls.Clear();
            clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.panel_customer_page);
        }

        private void toolStrip_make_order_Click(object sender, EventArgs e)
        {
            clear_content(ref this.tabPage1,ref  this.toolStrip2);
            this.tabPage1.Controls.Add(this.Url);
        }

        private void btn_flah_url_Click(object sender, EventArgs e)
        {
            clear_content(ref tabPage1, ref toolStrip2);
            this.tabPage1.Controls.Add(this.panel_order_information);
            string Flash_Url=Url.Text;

            this.tabPage1.Controls.Add(this.panel_order_information);      
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //SoundPlayer play = new SoundPlayer("C:\\Users\\AYMAN\\Desktop\\Windows Ringin.wav");
            //play.Play();
           
            string command = "Insert Into Person (Name,Phone,Address) values ('ayman','4414','wasta')";
           DataBase.insert_update_delete_in_database(command);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DataBase.close_connection();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlDataReader reader1 = DataBase.select_from_database("select * from Person_Kind");
            //DataTable dt = new DataTable();
            //dt.Load(reader1);
           // MessageBox.Show(reader1[1].ToString());

            while (reader1.Read())
            {
                MessageBox.Show(reader1[1].ToString());
            }
        }

        

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) //ayman sayed
        {
            if (combo_profile_exit_ques.SelectedIndex==0)  //have old account
            {
                panel_add_customer.Controls.Add(combo_select_oldname);
                panel_add_customer.Controls.Remove(lbl_owner_address);
                panel_add_customer.Controls.Remove(lbl_owner_name);
                panel_add_customer.Controls.Remove(lbl_owner_phone);
                panel_add_customer.Controls.Remove(lbl_studio_owner_line);
                panel_add_customer.Controls.Remove(txt_owner_address);
                panel_add_customer.Controls.Remove(txt_owner_name);
                panel_add_customer.Controls.Remove(txt_owner_phone);
                old_owner_exist = true;
                personsNames=TempEmployee.get_persons_names(studio_owner_type_id);
                combo_select_oldname.Items.Clear();
                foreach (string name in personsNames)
                {
                    combo_select_oldname.Items.Add(name);
                }
            }
            if (combo_profile_exit_ques.SelectedIndex == 1) //havn't old account
            {
                panel_add_customer.Controls.Remove(combo_select_oldname);
                panel_add_customer.Controls.Add(lbl_owner_address);
                panel_add_customer.Controls.Add(lbl_owner_name);
                panel_add_customer.Controls.Add(lbl_owner_phone);
                panel_add_customer.Controls.Add(lbl_studio_owner_line);
                panel_add_customer.Controls.Add(txt_owner_address);
                panel_add_customer.Controls.Add(txt_owner_name);
                panel_add_customer.Controls.Add(txt_owner_phone);
                old_owner_exist = false;

            }
        }

        private void combo_select_oldname_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btn_add_customer_Click(object sender, EventArgs e)//ayman sayed
        {
            int studio_return_id=0,owner_return_id=0;
            Studio_customer studio_object = new Studio_customer();
            Studio_Owner owner_object = new Studio_Owner();
            bool is_owner_field_empty = false;
            bool is_studio_field_empty = false;
            is_studio_field_empty = is_studio_fields_empty(ref panel_add_customer,txt_studio_name,txt_studio_address,txt_studio_phone);
            if (is_studio_field_empty==false)
            {
                studio_object.Name = txt_studio_name.Text;
                studio_object.Addres = txt_studio_address.Text;
                studio_object.phone = txt_studio_phone.Text;
                studio_object.Person_type_id = studio_type_id;
            }
            if (old_owner_exist == false)  //the customer registe for the first time
            {
               is_owner_field_empty= is_owner_fields_empty();
                if (txt_owner_name.Text != "" && txt_owner_phone.Text != "" && txt_owner_address.Text != "")
                {
                    owner_object.Name = txt_owner_name.Text;
                    owner_object.phone = txt_owner_phone.Text;
                    owner_object.Addres = txt_owner_address.Text;
                    owner_object.Person_type_id = studio_owner_type_id;
                }
            }

            if (old_owner_exist == true&&is_studio_field_empty==false)
            {
              owner_return_id=TempEmployee.get_person_id(combo_select_oldname.SelectedItem.ToString());
            }
            if (is_studio_field_empty == false && is_owner_field_empty==false)
            {
                if (old_owner_exist == false)
                {
                    owner_return_id = TempEmployee.AddPersonDB(owner_object);
                }
                studio_return_id = TempEmployee.AddPersonDB(studio_object);
                TempEmployee.Link_Studio_With_owner(studio_return_id, owner_return_id);
                MessageBox.Show("تم حفظ البيانات بنجاح");
            }
            
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            txt_owner_address.Text = "";
            txt_owner_name.Text = "";
            txt_owner_phone.Text = "";
            txt_studio_address.Text = "";
            txt_studio_name.Text = "";
            txt_studio_phone.Text = "";
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            if (login == false)
            {
                clear_content(ref this.tabPage1, ref this.toolStrip2);
                this.tabPage1.Controls.Add(this.panel_login);
            }
            else
            {
                login = false;
                toolStripButton_login.Text = "تسجيل الدخول";
            }
        }

        private void toolStrip_add_employee_Click(object sender, EventArgs e)
        {
            clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.Panel_Add_Employee);
        }

        private void btn_save_employee_data_Click(object sender, EventArgs e)
        {
            Employee emp = new Employee();
            emp.Name = txt_employee_name.Text;
            emp.Addres = txt_employee_address.Text;
            emp.Person_type_id = employee_type_id;
            emp.phone = txt_employee_phone_number.Text;
            emp.Salary = Convert.ToDouble(txt_employee_salary.Text);
            emp.Ssn = Convert.ToDouble(txt_employee_ssn.Text);
            emp.User_Name = txt_employee_user_name.Text;
            emp.PassWord = txt_employee_password.Text;
            emp.Work_Start_Time = DateTime.Today;
            emp.Work_State = work_state_active;
            emp.ID = manger_object.AddPersonDB(emp);  //add the prson data
            manger_object.add_employ_table(emp);
            MessageBox.Show("تم حفظ البيانات بنجاح");
        }

        private void panel_add_customer_Paint(object sender, PaintEventArgs e)
        {

        }

        private void toolStrip_change_username_password_Click(object sender, EventArgs e)
        {
            clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.panel_change_password);
        }

       

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.panel_stop_employee_working);
            combo_esrch_employee.Items.Clear();
            List<string> employees_name = manger_object.get_persons_names(employee_type_id);
            foreach (string name in employees_name)
            {
                combo_esrch_employee.Items.Add(name);
            }
        }

       

        private void btn_delete_employee_Click(object sender, EventArgs e)
        {
            if (combo_esrch_employee.Text == "")
            {
                MessageBox.Show("من فضلك ادخل اسم الموظف ");
            }
            else
            {
                string emp_name = combo_esrch_employee.SelectedItem.ToString();
                int emp_id=manger_object.get_person_id(emp_name);
                manger_object.update_employee_working_state(emp_id,work_state_notavtive);
                MessageBox.Show("لقد تم ايــقاف عمل الموظف بنجاح");
            }
        }

        private void toolStrip_delete_customer_Click(object sender, EventArgs e)
        {
            clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.panel_delete_studio);
            comboBox_delete_studio.Items.Clear();
            List<string> studios_name = manger_object.get_persons_names(studio_type_id);
            foreach (string name in studios_name)
            {
                comboBox_delete_studio.Items.Add(name);
            }
        }

        private void btn_delete_studio_Click(object sender, EventArgs e)
        {
            if (comboBox_delete_studio.Text == "")
            {
                MessageBox.Show("من فضلك ادخل اسم الاستديو ");
            }
            else
            {
                string studio_name = comboBox_delete_studio.SelectedItem.ToString();
                int studio_id = TempEmployee.get_person_id(studio_name);
               int delete_result= TempEmployee.Delete_studio(studio_id);
               comboBox_delete_studio.Items.Remove(studio_name);
               MessageBox.Show("لقد تم حذف الاستديو بنجاح");
               if (delete_result != -1)
               {
                   DialogResult res=  MessageBox.Show("مالك الاستديو لا يمتلك الان اى استديو اخر \n هل تريد حذفه؟", "تحذير", MessageBoxButtons.OKCancel);
                 if (res == DialogResult.OK)
                 {
                     TempEmployee.delete_studio_owner(delete_result);
                     MessageBox.Show("لقد تم حذف العميل بنجاح");
                 }
               }
               
            }
        }

        private void btn_change_password_Click(object sender, EventArgs e)
        {
            if (txt_new_password.Text == txt_confirm_password.Text)
            {
                bool pass_update_reslt = TempEmployee.change_password(TempEmployee.ID, txt_current_password.Text, txt_new_password.Text);
                if (pass_update_reslt)
                {
                    lbl_current_pass.Text = "";
                    lbl_confirm_new_pass.Text = "";
                    MessageBox.Show("تم حفظ التغيرات");
                }
                else
                {
                    lbl_current_pass.Text = "غير صحيح";
                }
            }
            else
                lbl_confirm_new_pass.Text = "غير مطابق";
        }

        private void toolStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
             clear_content(ref this.tabPage1, ref this.toolStrip2);
            this.tabPage1.Controls.Add(this.panel_delete_studio);
            comboBox_delete_studio.Items.Clear();
            List<string> studios_name = manger_object.get_persons_names(studio_type_id);
            foreach (string name in studios_name)
            {
                comboBox_delete_studio.Items.Add(name);
            }
        }

        private void Panel_Add_Employee_Paint(object sender, PaintEventArgs e)
        {

        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
          
            
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {
            
        }




        

        

        
       

       
      
        

       

       
       
    }
}
